<?php

require_once ('common.php');
$action = trim($_REQUEST['action']);
$topic_id = trim($_REQUEST['id']);
if (!empty($action))
    checkLogin();
if ($action == 'add')
{
    $topic_title = trim(htmlspecialchars(stripslashes($_POST['topic_title'])));
    $topic_text = trim(htmlspecialchars($_POST['topic_text']));
    $topic_poster = $uid;
    $topic_time = getTime();
    $topic_icon = trim(htmlspecialchars($_POST['topic_icon']));
    if (addTopic($topic_title, $topic_text, $topic_poster, $topic_time, $topic_icon))
    {
        $alltopics = getAllTopics();
        foreach ($alltopics as $t)
        {
            if ($t['topic_poster'] == $uid)
            {
                $topic_id = $t['topic_id'];
                break;
            }
        }
        header('location: ' . APP_URL . '/topic/' . $topic_id);
    }
    else
        header('location: ' . APP_URL . '/newtopic');
    exit;
} elseif ($action == 'edit')
{
    if (empty($topic_id))
    {
        header('location: ' . APP_URL);
        exit;
    }
    $topic_title = trim(htmlspecialchars(stripslashes($_POST['topic_title'])));
    $topic_text = trim(htmlspecialchars(stripslashes($_POST['topic_text'])));
    $topic_editor = $uid;
    $edit_time = getTime();
    $topic_icon = trim(htmlspecialchars(stripslashes($_POST['topic_icon'])));
    editTopic($topic_id, $topic_title, $topic_text, $topic_editor, $edit_time, $topic_icon);
    header('location: ' . APP_URL . '/topic/' . $topic_id);
    exit;
} elseif ($action == 'delete')
{
    if (empty($topic_id))
    {
        header('location: ' . APP_URL);
        exit;
    }
    $topic_deletor = $uid;
    deleteTopic($topic_id, $topic_deletor);
    header('location: ' . APP_URL);
    exit;
}
if (!empty($topic_id))
{
    $topics = getTopic($topic_id);
    $posts = getPosts($topic_id);
    $topic_gift = '';
    if (!$topics)
    {
        header('location: ' . APP_URL);
        exit;
    }
    $topics['can_gift'] = true;
    if (!empty($topics['gift']))
    {
        foreach (explode(',', $topics['gift']) as $k => $v)
        {
            $profile = getUserProfile($v);
            $sep = ($k == 0) ? '' : ', ';
            $topic_gift .= $sep . '<a href="' . APP_URL . '/user/' . $profile['uid'] . '" title="ดูโปรไฟล์ของ ' . $profile['realname'] . '">' . $profile['realname'] . '</a>';
        }
        if (in_array($uid, explode(',', $topics['gift'])))
            $topics['can_gift'] = false;
        else
            $topics['can_gift'] = true;
    }
}
else
{
    header('location: ' . APP_URL);
    exit;
}
$emos = $bbcode->getEmos();
$pagetitle = $topics['topic_title'];
$share2twitter = 'http://twitter.com?status=' . urlencode($pagetitle . ' ' . APP_URL . '/topic/' . $topics['topic_id']);
$share2facebook = 'http://www.facebook.com/share.php?u=' . APP_URL . '/topic/' . $topics['topic_id'] . '&t=' . $pagetitle;
$smarty->assign('pagetitle', $pagetitle);
$smarty->display('header.html');
$smarty->assign('uid', $uid);
$smarty->assign('topics', $topics);
$smarty->assign('share2twitter', $share2twitter);
$smarty->assign('share2facebook', $share2facebook);
$smarty->assign('gift_count', count(explode(',', $topics['gift'])));
$smarty->assign('topic_gift', $topic_gift);
$smarty->assign('posts', $posts);
$smarty->assign('posts_count', empty($posts) ? 0 : count($posts));
$smarty->assign('emos', $emos);
$smarty->display('topic.html');
require_once ('sidebar.php');
require_once ('footer.php');

?>